package BehaviorModel.commandModel;

public class CommandController {
    // 开启命令组
    Command[] onCommands;
    // 关闭命令组
    Command[] offCommands;

    // 上一次执行的命令
    Command lastCommand;

    public CommandController(){
        onCommands = new Command[5];
        offCommands = new Command[onCommands.length];

        // 初始化 命令组 为 空实现
        for (int index = 0; index < onCommands.length; index++) {
            onCommands[index] = new UnCommand();
            offCommands[index] = new UnCommand();
        }
    }

    /**
     * 功能描述: 设置指定索引的命令组
     *
     * @param index
     * @param onCommand
     * @param offCommand
     */
    public void setCommand(int index,Command onCommand,Command offCommand){
        onCommands[index] = onCommand;
        offCommands[index] = offCommand;
    }

    /**
     * 功能描述: 开启
     *
     * @param index
     */
    public void on(int index){
        onCommands[index].execute();
        lastCommand = onCommands[index];
    }

    /**
     * 功能描述: 关闭
     *
     * @param index
     */
    public void off(int index){
        offCommands[index].execute();
        lastCommand = offCommands[index];
    }

    /**
     * 功能描述: 撤销
     */
    public void undo(){
        lastCommand.undo();
    }
}
